package com.whj.servicedriveruser.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.whj.internalcommon.constant.CommonStatusEnum;
import com.whj.internalcommon.constant.DriverCarConstants;
import com.whj.internalcommon.dto.DriverCarBindingRelationship;
import com.whj.internalcommon.dto.DriverUser;
import com.whj.internalcommon.dto.ResponseResult;
import com.whj.servicedriveruser.mapper.DriverCarBindingRelationshipMapper;
import com.whj.servicedriveruser.mapper.DriverUserMapper;
import com.whj.servicedriveruser.service.IDriverCarBindingRelationshipService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author whj
 * @since 2023-01-13
 */
@Service
public class DriverCarBindingRelationshipServiceImpl implements IDriverCarBindingRelationshipService {
    @Autowired
    private DriverCarBindingRelationshipMapper driverCarBindingRelationshipMapper;

    @Autowired
    private DriverUserMapper driverUserMapper;

    /**
     * 根据司机id和车辆id相互绑定
     *
     * @param driverCarBindingRelationship 司机id和车辆id
     * @return
     */
    @Override
    public ResponseResult bind(DriverCarBindingRelationship driverCarBindingRelationship) {

        // 判断，如果参数中的车辆和司机，已经做过绑定，则不允许再次绑定
        QueryWrapper<DriverCarBindingRelationship> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("driver_id", driverCarBindingRelationship.getDriverId());
        queryWrapper.eq("car_id", driverCarBindingRelationship.getCarId());
        queryWrapper.eq("bind_state", DriverCarConstants.DRIVER_CAR_BIND);

        Long integer = driverCarBindingRelationshipMapper.selectCount(queryWrapper);
        System.out.println("integer = " + integer);
        if ((integer.intValue() > 0)) {
            return ResponseResult.fail(CommonStatusEnum.DRIVER_CAR_BIND_EXISTS.getCode(), CommonStatusEnum.DRIVER_CAR_BIND_EXISTS.getValue());
        }

        // 司机被绑定了
        queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("driver_id", driverCarBindingRelationship.getDriverId());
        queryWrapper.eq("bind_state", DriverCarConstants.DRIVER_CAR_BIND);
        integer = driverCarBindingRelationshipMapper.selectCount(queryWrapper);
        if ((integer.intValue() > 0)) {
            return ResponseResult.fail(CommonStatusEnum.DRIVER_BIND_EXISTS.getCode(), CommonStatusEnum.DRIVER_BIND_EXISTS.getValue());
        }

        // 车辆被绑定了
        queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("car_id", driverCarBindingRelationship.getCarId());
        queryWrapper.eq("bind_state", DriverCarConstants.DRIVER_CAR_BIND);
        integer = driverCarBindingRelationshipMapper.selectCount(queryWrapper);
        if ((integer.intValue() > 0)) {
            return ResponseResult.fail(CommonStatusEnum.CAR_BIND_EXISTS.getCode(), CommonStatusEnum.CAR_BIND_EXISTS.getValue());
        }

        LocalDateTime now = LocalDateTime.now();
        driverCarBindingRelationship.setBindingTime(now);

        driverCarBindingRelationship.setBindState(DriverCarConstants.DRIVER_CAR_BIND);

        driverCarBindingRelationshipMapper.insert(driverCarBindingRelationship);
        return ResponseResult.success("");
    }

    /**
     * 根据司机id和车辆id解除绑定
     *
     * @param driverCarBindingRelationship 司机id和车辆id
     * @return
     */
    @Override
    public ResponseResult unbind(DriverCarBindingRelationship driverCarBindingRelationship) {

        Map<String, Object> map = new HashMap<>();
        map.put("driver_id", driverCarBindingRelationship.getDriverId());
        map.put("car_id", driverCarBindingRelationship.getCarId());
        map.put("bind_state", DriverCarConstants.DRIVER_CAR_BIND);
        List<DriverCarBindingRelationship> driverCarBindingRelationships = driverCarBindingRelationshipMapper.selectByMap(map);

        if (driverCarBindingRelationships.isEmpty()) {
            return ResponseResult.fail(CommonStatusEnum.DRIVER_CAR_BIND_NOT_EXISTS.getCode(), CommonStatusEnum.DRIVER_CAR_BIND_NOT_EXISTS.getValue());
        }

        DriverCarBindingRelationship relationship = driverCarBindingRelationships.get(0);
        relationship.setBindState(DriverCarConstants.DRIVER_CAR_UNBIND);

        LocalDateTime now = LocalDateTime.now();
        relationship.setUnBindingTime(now);
        driverCarBindingRelationshipMapper.updateById(relationship);
        return ResponseResult.success("");
    }

    /**
     * 根据司机手机号查询车辆信息
     *
     * @param driverPhone
     * @return
     */
    @Override
    public ResponseResult<DriverCarBindingRelationship> findMessage(String driverPhone) {

        QueryWrapper<DriverUser> driverQueryWrapper = new QueryWrapper<>();
        driverQueryWrapper.eq("driver_phone",driverPhone);
        DriverUser driverUser = driverUserMapper.selectOne(driverQueryWrapper);
        Long driverId = driverUser.getId();
        QueryWrapper<DriverCarBindingRelationship> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("driver_id",driverId);
        queryWrapper.eq("bind_state",DriverCarConstants.DRIVER_CAR_BIND);
        DriverCarBindingRelationship driverCarBindingRelationship = driverCarBindingRelationshipMapper.selectOne(queryWrapper);
        if (driverCarBindingRelationship == null) {
            return ResponseResult.fail(CommonStatusEnum.DRIVER_CAR_BIND_NOT_EXISTS.getCode(), CommonStatusEnum.DRIVER_CAR_BIND_NOT_EXISTS.getValue(), "");
        }

        return ResponseResult.success(driverCarBindingRelationship);
    }
}
